package com.shencai.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shencai.demo.base.entity.SysMenu;
import com.shencai.demo.base.service.SysMenuService;
import com.shencai.demo.base.mapper.SysMenuMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sckj
 * @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
 * @createDate 2022-10-19 11:04:48
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
        implements SysMenuService{
    /**
     * 查询菜单树
     * @return
     */
    @Override
    public List<SysMenu> listMenuTree() {
        /*
        1.获取所有菜单
        2.过滤一级菜单
        3.递归查询子菜单后排序
         */
        List<SysMenu> menus = baseMapper.selectList(null);

        List<SysMenu> menuTree = menus.stream().filter(menu ->
                menu.getParentId() == 0
        ).map((menu) -> {
            menu.setMenuChildren(getMenuChildren(menus, menu));
            return menu;
        }).sorted((menu1, menu2) -> {
            return Integer.compare(menu1.getOrderNum(), menu2.getOrderNum());
        }).collect(Collectors.toList());
        return menuTree;
    }

    /**
     * 获取子菜单
     * @param menus 所有的菜单
     * @param menuRoot 根菜单，用于查询子菜单
     * @return
     */
    List<SysMenu> getMenuChildren(List<SysMenu> menus, SysMenu menuRoot){
        List<SysMenu> menuChildrens = menus.stream().filter(menu ->
                menu.getParentId().equals(menuRoot.getId())
        ).map((menu) -> {
            menu.setMenuChildren(getMenuChildren(menus, menu));
            return menu;
        }).sorted((menu1, menu2) -> {
            return Integer.compare(menu1.getOrderNum(), menu2.getOrderNum());
        }).collect(Collectors.toList());
        return menuChildrens;
    }

    /**
     * 根据菜单ID获取下一级菜单
     * @param id
     * @return
     */
    @Override
    public List<SysMenu> listNextMenus(Integer id) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getParentId, id);
        List<SysMenu> sysMenus = baseMapper.selectList(queryWrapper);
        return sysMenus;
    }

    /**
     * 根据菜单ID分页获取下一级菜单
     * @param id
     * @return
     */
    @Override
    public Page<SysMenu> listNextMenusByPage(Integer id, Integer currentPage, Integer pageSize) {
        Page<SysMenu> menuPage = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getParentId, id);
        baseMapper.selectPage(menuPage,queryWrapper);
        List<SysMenu> collect = menuPage.getRecords().stream().sorted((menu1, menu2) -> {
            return Integer.compare(menu1.getOrderNum(), menu2.getOrderNum());
        }).collect(Collectors.toList());
        menuPage.setRecords(collect);
        return menuPage;
    }

    /**
     * 根据菜单名称和菜单编号模糊查询
     * @return
     */
    @Override
    public List<SysMenu> ListMenuByNameAndCode(String name, String code) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SysMenu::getName,name == null ? "":name).like(SysMenu::getCode,code == null ? "":code);
        List<SysMenu> menus = baseMapper.selectList(queryWrapper);
        return menus;
    }

    /**
     * 根据菜单名和菜单编码模糊分页查询菜单
     * @param name
     * @param code
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<SysMenu> ListMenuByNameAndCodeBypage(String name, String code, Integer currentPage, Integer pageSize) {
        Page<SysMenu> menuPage = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SysMenu::getName,name == null ? "":name).like(SysMenu::getCode,code == null ? "":code);
        baseMapper.selectPage(menuPage, queryWrapper);
        List<SysMenu> collect = menuPage.getRecords().stream().sorted((menu1, menu2) -> {
            return Integer.compare(menu1.getOrderNum(), menu2.getOrderNum());
        }).collect(Collectors.toList());
        menuPage.setRecords(collect);
        return menuPage;
    }

    /**
     * 添加菜单，如果菜单名和菜单编码重复则不保存
     * @param menu
     * @return
     */
    @Override
    public boolean saveMenu(SysMenu menu) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getCode,menu.getCode()).or().eq(SysMenu::getName,menu.getName());
        SysMenu sysMenu = baseMapper.selectOne(queryWrapper);
        if (sysMenu == null){
            return this.save(menu);
        }
        return false;
    }

    /**
     * 删除菜单,如果有子菜单，不删除
     * @param id
     * @return
     */
    @Override
    public boolean removeMenuById(Integer id) {
        List<SysMenu> menus = listNextMenus(id);
        if (menus.size()!=0) {
            return false;
        }
        return this.removeById(id);
    }
}




